from conan import ConanFile
from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout
from conan.tools.files import copy, get,chdir
from conan.tools.gnu import Autotools, AutotoolsToolchain
import os
class clogRecipe(ConanFile):
    name = "clog"  # name of the package
    version = "2.0.0"  # version of the package
    license = "GPL-2.0"# license of the package
    author = "Liuzhiyu"# author of the package
    #url = "https://github.com/michaelrsweet/mxml"# url of the package
    description = ("This is an encapsulated C++ log library that encapsulates several open source libraries and provides a unified interface.")
    topics = ("C++", "glog", "log","log4cplus", "log" "library")# topics of the package
    settings = "os", "compiler", "build_type", "arch"# settings of the package
    options = {"shared": [True, False], "fPIC": [True, False]}# options of the package
    default_options = {"shared": False, "fPIC": True}# default options of the package
    exports_sources = "libs_src/*" 


    def source(self):# source of the package
    #     # Please, be aware that using the head of the branch instead of an immutable tag
    #     # or commit is a bad practice and not allowed by Conan
    #     get(self, "https://github.com/michaelrsweet/mxml/archive/refs/tags/v3.3.1.zip",
    #               strip_root=True)
        self.output.info("self.source_folder" + self.source_folder)
        cmake_file_path = os.path.join(self.source_folder, "CMakeLists.txt")
        if os.path.exists(cmake_file_path):
            self.output.info("CMakeLists.txt exists: Yes")
        else:
            self.output.error("CMakeLists.txt exists: No")
            # 如果不加权限,configure()方法有可能无法执行
            os.chmod(os.path.join(self.source_folder, "configure"), 0o755)

    def requirements(self):
        self.requires("gflags/2.2.2")
        self.requires("log4cplus/2.1.1")
        self.requires("glog/0.7.0")
    def config_options(self):
        if self.settings.os == "Windows":
            del self.options.fPIC # Windows does not require -fPIC flag

    def layout(self):
  #      cmake_layout(self)
        self.folders.source = "libs_src/clog/"

        
    def generate(self):
        if os.path.exists(os.path.join(self.source_folder, "CMakeLists.txt")):
            tc = CMakeToolchain(self)
            tc.variables["BUILD_SHARED_LIBS"] = "ON" if self.options.shared else "OFF"
            if self.settings.os != "Windows":
                tc.variables["CMAKE_POSITION_INDEPENDENT_CODE"] = "ON" if self.options.fPIC else "OFF"
        else:
            tc = AutotoolsToolchain(self)
        tc.generate()

    def build(self):
        if os.path.exists(os.path.join(self.source_folder, "CMakeLists.txt")):
            # CMake 构建流程
            self.output.info("执行 CMake 构建流程")
            cmake = CMake(self)
            cmake.configure(variables={"LIBLOG_VERSION": "2.0.0"})
            cmake.build()
        else:
            with chdir(self, self.source_folder):
                autotools = Autotools(self)
                autotools.configure(args=["--prefix={}".format(self.package_folder)])
                autotools.make()
    def package(self):
        if os.path.exists(os.path.join(self.source_folder, "CMakeLists.txt")):
            cmake = CMake(self)
            cmake.install()
        else:
            with chdir(self, self.source_folder):
                autotools = Autotools(self)
                autotools.install()
            
    def package_info(self):
        self.cpp_info.libs = ["clog"]